package com.bocai.dahuatech.utils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class ShareUtil implements PlatformActionListener {
    private Context context;
    private String text;
    private String imagepath;
    public void showShare(Context context,String what,String imagepath,String title,String text,String url){
        this.context=context;
        this.imagepath=imagepath;
        this.text=text;
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setImageUrl(imagepath);
        sp.setTitle(title);
        sp.setTitleUrl(url); // 标题的超链接
        sp.setText(text);
        sp.setUrl(url);
        sp.setShareType(Platform.SHARE_WEBPAGE);
        whatShow(what, sp);
    }

    private void whatShow(String what,Platform.ShareParams sp) {
        switch (what){
            //QQ
            case "qq":
                Platform qq = ShareSDK.getPlatform(context, QQ.NAME);
                qq.setPlatformActionListener(this);
                qq.share(sp);
                break;
            //QQ空间
            case "qqzone":
                Platform qqzone = ShareSDK.getPlatform(context, QZone.NAME);
                qqzone.setPlatformActionListener(this);
                qqzone.share(sp);
                break;
            //新浪微博
            case "sinaweibo":
                Platform sinaweibo  = ShareSDK.getPlatform(SinaWeibo.NAME);
                sinaweibo.setPlatformActionListener(this);
                sinaweibo.share(sp);
                break;
            //微信
            case "weixin":
                Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
                wechat.setPlatformActionListener(this);
                wechat.share(sp);
                break;
            //微信朋友圈
            case "weixinfriend":
                Platform weixinfriend = ShareSDK.getPlatform(WechatMoments.NAME);
                weixinfriend.setPlatformActionListener(this);
                weixinfriend.share(sp);
                break;
        }
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        String name=platform.getName();
        if("SinaWeibo".equals(name)){
            Toast.makeText(context, "正在后台操作", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
        Log.i("zhang", throwable.toString());
    }

    @Override
    public void onCancel(Platform platform, int i) {
        Toast.makeText(context, "取消分享", Toast.LENGTH_SHORT).show();
    }
}
